package com.bjsxt.service.impl;


import com.alibaba.druid.util.MapComparator;
import com.bjsxt.mapper.QuestionDao;
import com.bjsxt.mapper.QuestionOptDao;
import com.bjsxt.pojo.Question;
import com.bjsxt.pojo.QuestionOpt;
import com.bjsxt.service.QuestionServer;
import com.bjsxt.utils.BeanMapUtils;
import com.bjsxt.utils.MapParameter;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Splitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class QuestionServerImpl implements QuestionServer {

    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private QuestionOptDao questionOptDao;

    @Override
    public int create(Question question) {

        int falg = 0;

        if (question.getId() != null) {
            falg = this.update(question);
            // questionDao.delete(MapParameter.getInstance().put("id",question.getId()).getMap());
            questionOptDao.delete(MapParameter.getInstance().put("questionId", question.getId()).getMap());
        } else {
            falg = questionDao.create(question);
        }

        if (falg > 0) {
            List<QuestionOpt> options = question.getOptions();
            int i = 0;
            for (QuestionOpt option : options) {
                option.setSurveyId(question.getSurveyId());
                option.setQuestiondId(question.getId());
                option.setOrderby(++i);
                questionOptDao.create(option);
            }
            //如果新建成功则把新建的问题id进行返回
            return question.getId();
        }
        //如果新建失败则把0进行返回
        return falg;
    }

    //根据问题id（questionId）进行删除

    /**
     * 只有在问题成功删除后才能删除后问题选项，最后返回选项删除的int标识。
     * @param id
     * @return int
     */
    @Override
    public int delete(String id) {
        int flag= 0;
        int flagOpt= 0;

        flag = questionDao.delete(MapParameter.getInstance().put("id", id).getMap());

        if (flag>0){
            flagOpt =questionOptDao.delete(MapParameter.getInstance().put("questionId",id).getMap());
        }else {
            return flag;
        }

        return flagOpt;
    }

    @Override
    public int update(Question question) {
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMapForUpdate(question)).getMap();
        map.put("id", question.getId());
        System.out.println(map.toString());
        return questionDao.update(map);
    }

    /**
     * deleteBatch 处理批量删除操作
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String ids) {
        int flag = 0;
        List<String> list = Splitter.on(",").splitToList(ids);
        for (String s : list) {
            questionDao.delete(MapParameter.getInstance().put("id", Integer.parseInt(s)).getMap());
            flag++;
        }
        return flag;
    }

    @Override
    public List<Question> query(Question question) {

        Map<String, Object> map = BeanMapUtils.beanToMap(question);
        //查询当个问卷的全部问题
        List<Question> questions = questionDao.query(map);
        //查询当个问卷全部问题的选项
        List<QuestionOpt> questionOpts = questionOptDao.query(MapParameter.getInstance().put("surveyID", question.getSurveyId()).getMap());

        for (Question question1 : questions) {
            List<QuestionOpt> list = new ArrayList<>();
            for (QuestionOpt questionOpt : questionOpts) {
                if (question1.getId()==questionOpt.getQuestiondId()){
                    list.add(questionOpt);
                }
            }
            question1.setOptions(list);
        }
        return questions;
    }

    @Override
    public Question detail(Question question) {

        return questionDao.detail(BeanMapUtils.beanToMap(question));
    }

    @Override
    public int count(Question question) {

        return questionDao.count(BeanMapUtils.beanToMap(question));
    }


}
